package com.demo.controller.media;

import com.demo.model.Media;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * @Auther: 10091
 * @Date: 2018-07-14 09:16
 * @Description: 媒体管理
 */
public class MediaAdminService {

    public final  static  MediaAdminService me = new MediaAdminService();
    private Media dao = new Media().dao();

    /**
     * 分页查询
     * @param pageNum
     * @return
     */
    public Page<Media> paginate(int pageNum){
        return dao.paginate(pageNum,10,"select *"," from f_media order by create_date desc");
    }

    /**
     * 创建媒体
     */
    public Ret save( Media media) {
        media.setMediaName(media.getMediaName().trim());
        media.setFansNum(0);
        media.setOpenTake(0);
        media.save();
        return Ret.ok("msg", "创建成功");
    }
    public Media edit(int id) {
        return dao.findById(id);
    }

    public Ret update(Media media) {
        media.update();
        return Ret.ok("msg", "修改成功");
    }

    public Ret lock(int id) {
        Db.update("update f_media set open_take = 1 where id=?", id);
        return Ret.ok("msg", "锁定成功");
    }

    public Ret unlock(int id) {
        Db.update("update f_media set open_take = 0 where id=?", id);
        return Ret.ok("msg", "解除锁定成功");
    }

    /**
     * 删除 f_media
     */
    public Ret delete(int mediaId) {
        Integer accountId = Db.queryInt("select id from f_media where id=? limit 1", mediaId);
        if (accountId != null) {
            Db.update("delete from  f_media where id=?",mediaId);
            return Ret.ok("msg", "f_media 删除成功");
        } else {
            return Ret.fail("msg", "f_media 删除失败");
        }
    }

    /**
     * 查询全部可用的媒体号
     * @return
     */
    public List<Media> findAllNoLock(){
        return dao.find("select * from f_media where open_take = 0 ");
    }


}
